package com.itany.ems.service.impl;

import com.itany.ems.dao.EmpDao;
import com.itany.ems.dao.impl.EmpDaoImpl;
import com.itany.ems.entity.Emp;
import com.itany.ems.exception.DataAccessException;
import com.itany.ems.exception.RequestParameterErrorException;
import com.itany.ems.exception.ServiceException;
import com.itany.ems.service.EmpService;
import com.itany.ems.util.ParameterUtil;

import java.util.List;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/3/3 13:59
 * Version:1.0-SNAPSHOT
 */
public class EmpServiceImpl implements EmpService {

    private EmpDao empDao = new EmpDaoImpl();

    @Override
    public void addEmp(String name, String salary, String gender, String edu, String[] hobbies) throws RequestParameterErrorException, ServiceException {
        if(ParameterUtil.isNull(name) || ParameterUtil.isNull(salary) || ParameterUtil.isNull(gender) || ParameterUtil.isNull(edu) || null == hobbies){
            throw new RequestParameterErrorException("请求参数有误");
        }
        Emp emp = new Emp(name,Double.parseDouble(salary),gender,edu,hobbies);
        try {
            empDao.insertEmp(emp);
        } catch (DataAccessException e) {
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    public List<Emp> findAll() throws ServiceException {
        try {
            List<Emp> emps = empDao.selectAll();
            return emps;
        } catch (DataAccessException e) {
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    public Emp findById(String id) throws RequestParameterErrorException, ServiceException {
        if(ParameterUtil.isNull(id)){
            throw new RequestParameterErrorException("请求参数有误");
        }
        try {
            Emp emp = empDao.selectById(Integer.parseInt(id));
            return emp;
        } catch (DataAccessException e) {
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    public void modifyEmp(String id, String name, String salary, String gender, String edu, String[] hobbies) throws RequestParameterErrorException, ServiceException {
        if(ParameterUtil.isNull(id) || ParameterUtil.isNull(name) || ParameterUtil.isNull(salary) || ParameterUtil.isNull(gender) || ParameterUtil.isNull(edu) || null == hobbies){
            throw new RequestParameterErrorException("请求参数有误");
        }
        Emp emp = new Emp(Integer.parseInt(id),name,Double.parseDouble(salary),gender,edu,hobbies);
        try {
            empDao.updateEmp(emp);
        } catch (DataAccessException e) {
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    public void removeEmp(String id) throws RequestParameterErrorException, ServiceException {
        if(ParameterUtil.isNull(id)){
            throw new RequestParameterErrorException("请求参数有误");
        }
        try {
            empDao.deleteById(Integer.parseInt(id));
        } catch (DataAccessException e) {
            throw new ServiceException(e.getMessage());
        }
    }
}
